<script type="text/javascript">
	$(document).ready(function() {
		document.title = "Mapa de Produtividade de Milho";

		// gera os mapas da aplicação requisitada após o algoritmo ser executado
		generateMaps();
	});

	function generateMaps() {
		OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";
		OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
		OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
		var map;
		var options = {
			projection: new OpenLayers.Projection("EPSG:900913"),
			displayProjection: new OpenLayers.Projection("EPSG:4326"),
			// map horizontal units are meters
			units: "m",
			// this resolution displays the globe in one 256x256 pixel tile
			maxResolution: 156543.0339,
			// these are the bounds of the globe in sperical mercator
			maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
			20037508, 20037508),
			controls: [new OpenLayers.Control.Navigation(),
				new OpenLayers.Control.PanZoomBar()]
		};
		map = new OpenLayers.Map('small_map',options);
		map.addControl(new OpenLayers.Control.LayerSwitcher());

		// cria as Layers do Google Maps
		var gphy = new OpenLayers.Layer.Google(
			"Google Physical",
			{type: G_PHYSICAL_MAP,'sphericalMercator': true}
		);
		var gmap = new OpenLayers.Layer.Google(
			"Google Street", // the default
			{'sphericalMercator': true}
		);
		var ghyb = new OpenLayers.Layer.Google(
			"Google Hybrid",
			{type: G_HYBRID_MAP,'sphericalMercator': true}
		);
		var gsat = new OpenLayers.Layer.Google(
			"Google Satellite",
			{type: G_SATELLITE_MAP,'sphericalMercator': true}
		);

		var jacto = new OpenLayers.Layer.WMS(
			"Produtividade de Milho",
			"http://143.107.164.103:8081/cgi-bin/mapserv.exe?map=c:\\maps\\wms_jacto.map",
			{
				layers: "jacto",
				styles: '',
				srs: 'EPSG:4269',
				format: 'image/png',
				tiled: 'true',
				transparent: true
			},
			{
				'opacity': 0.75,
				'isBaseLayer': false,
				'wrapDateLine': true
			}
		);

		var vectors = new OpenLayers.Layer.Vector(
			"Marcadores",
			{isBaseLayer: false}
		);

		// adiciona as Layers no Mapa Após do Algoritmo Filtering
		map.addLayers([ gphy,
                                gmap,
                                ghyb,
                                gsat,
                                jacto,
                                vectors]);

//		var info = new OpenLayers.Control.WMSGetFeatureInfo({
//			url: "http://143.107.164.103:8081/cgi-bin/mapserv.exe?map=c:\\maps\\wms_postgis.map",
//			title: 'Identify features by clicking',
//			queryVisible: true,
//			vendorParams: {
//				"FEATURE_COUNT": "1000"},
//			eventListeners: {
//				getfeatureinfo:
//					function(event) {
//					if((event.text).length != 1) {
//						map.addPopup(new OpenLayers.Popup.FramedCloud(
//						"chicken",
//						map.getLonLatFromPixel(event.xy),
//						null,
//						event.text,
//						null,
//						true
//					));
//					}
//				}
//			}
//		});
//		map.addControl(info);
//		info.activate();

		var report = function(e) {
			OpenLayers.Console.log(e.type, e.feature.id);
		};

		var highlightCtrl = new OpenLayers.Control.SelectFeature(vectors, {
			hover: true,
			highlightOnly: true,
			renderIntent: "temporary",
			eventListeners: {
				beforefeaturehighlighted: report,
				featurehighlighted: report,
				featureunhighlighted: report
			}
		});

		var selectCtrl = new OpenLayers.Control.SelectFeature(vectors,
			{clickout: true}
		);

		map.events.register("click", map,
		function(e) {
			// Read the map coordinates from the click event
			var lonlat = map.getLonLatFromViewPortPx(e.xy).transform(map.projection,map.displayProjection);
			// Construct the query URL
			var url = "ClickQueryJacto.php";
			url += "?lon=" + lonlat.lon;
			url += "&lat=" + lonlat.lat;

			// Load the URL into an iframe
			document.getElementById("small_query").src = url;
		});

		map.addControl(highlightCtrl);
		map.addControl(selectCtrl);
		map.addControl(new OpenLayers.Control.EditingToolbar(vectors));
		map.addControl(new OpenLayers.Control.Scale($('small_scale')));
		map.addControl(new OpenLayers.Control.MousePosition({div: document.getElementById("small_location")}));
		map.addControl(new OpenLayers.Control.KeyboardDefaults);
		var panel = new OpenLayers.Control.Panel(
			{div: document.getElementById("small_panel")}
		);
		map.addControl(panel);
		var history = new OpenLayers.Control.NavigationHistory();
		map.addControl(history);
		panel.addControls(
			[history.next,
				history.previous]
		);
		var longitude = -50.01526; // jacto
		var latitude = -22.67936;  // jacto
		var zoom = 15;
		map.setCenter(new OpenLayers.LonLat(longitude, latitude).transform(map.displayProjection, map.projection), zoom);
		highlightCtrl.activate();
		selectCtrl.activate();
	}
</script>

<h1 class="title"><a id="jacto_app" href="#">Mapa de Produtividade de Milho</a></h1>

<div id="small_map_region">
    <div id="small_map"></div>
    <div id="small_location">location</div>
    <div id="small_panel"></div>
    <iframe id="small_query"></iframe>
</div>